from pyautogui import scroll
from pywinauto.keyboard import send_keys

from constants.constant_config import APP_PATH, APP_DIR, MAIN_WINDOW
from utils.program import Program

"""
ICD信号创建
"""

# 启动程序
program = Program(APP_PATH, APP_DIR, MAIN_WINDOW)
app = program.connect_program()
main_win = app.window(title=MAIN_WINDOW)


# # 点击域信息
def create_new_signal(byte_name, signal_type, signal_name, msg_id, byte_name_index=0):
    # 点击空白处新建信息
    main_win.child_window(title=byte_name, control_type="TreeItem", found_index=byte_name_index).double_click_input()
    new_win = main_win.child_window(title="新建域", control_type="Window")
    new_win.child_window(title=signal_type, control_type="ListItem").click_input()
    if signal_type == "占位域":
        # 输入位长
        new_win.child_window(title="", control_type="Spinner", found_index=2).click_input()
        send_keys("1")
        signal_name_control = new_win.child_window(title="信号名称*", control_type="Text")
        program.set_text(signal_name_control, signal_name)
        msg_id_control = new_win.child_window(title="信号ID*", control_type="Text")
        program.set_text(msg_id_control, msg_id)
    elif signal_type == "套表域":
        # 控制字信号名
        signal_name = new_win.child_window(title="控制字数据类型：", control_type="Text")
        test = program.set_text(signal_name, "测试套表域控制字")
        # 控制字信号ID
        program.set_text(test, "testTB")
        # 输入套表名称
        new_win.child_window(title="", control_type="Edit", found_index=4).click_input()
        send_keys("1")
        scroll(-100)
        new_win.child_window(title="添加", control_type="Button").click_input()
    else:
        signal_name_control = new_win.child_window(title="信号名称*", control_type="Text")
        program.set_text(signal_name_control, signal_name)
        msg_id_control = new_win.child_window(title="信号ID*", control_type="Text")
        program.set_text(msg_id_control, msg_id)

    new_win.child_window(title="确认", control_type="Button").click_input()
    main_win.child_window(title=byte_name, control_type="TreeItem", found_index=byte_name_index).click_input()
    scroll(-50)


# 1字节对齐方式创建所有信号
def create_all_single(icd_type, align_way):
    if icd_type == "1394CC":
        # 1字节对齐
        if align_way == "1":
            create_new_signal("24", "整型域", "测试整型域", "testInt", 1)
            create_new_signal("25", "Bit域", "测试Bit域", "testBit", 0)
            create_new_signal("26", "组合域", "测试组合域", "testZH", 0)
            create_new_signal("28", "Float域", "测试Float域", "testFloat", 0)
            create_new_signal("32", "布尔域", "测试布尔域", "testBool", 2)
            create_new_signal("32", "枚举域", "测试枚举域", "testEnum", 2)
            create_new_signal("32", "标签域", "测试标签域", "testLable", 3)
            create_new_signal("33", "字节域", "测试字节域", "testByte", 0)
            create_new_signal("34", "Double域", "测试字Double域", "testDouble", 0)
            create_new_signal("42", "BCD域", "测试字BCD域", "testBCD", 0)
            create_new_signal("43", "ASCII域", "测试字ASCII域", "testASCII", 0)
            create_new_signal("44", "字符域", "测试字符域", "testChar", 0)
            create_new_signal("45", "Unicode域", "测试Unicode域", "testUnicode", 0)
            create_new_signal("49", "占位域", "测试占位域", "testSpace", 0)
            create_new_signal("51", "套表域", "测试套表域", "testTB", 0)
        # 2字节对齐
        elif align_way == "2":
            create_new_signal("24", "整型域", "测试整型域", "testInt", 1)
            create_new_signal("26", "Bit域", "测试Bit域", "testBit", 0)
            create_new_signal("28", "组合域", "测试组合域", "testZH", 0)
            create_new_signal("30", "Float域", "测试Float域", "testFloat", 0)
            create_new_signal("34", "布尔域", "测试布尔域", "testBool", 0)
            create_new_signal("36", "枚举域", "测试枚举域", "testEnum", 0)
            create_new_signal("38", "标签域", "测试标签域", "testLable", 0)
            create_new_signal("40", "字节域", "测试字节域", "testByte", 0)
            create_new_signal("42", "Double域", "测试字Double域", "testDouble", 0)
            create_new_signal("50", "BCD域", "测试字BCD域", "testBCD", 0)
            create_new_signal("52", "ASCII域", "测试字ASCII域", "testASCII", 0)
            create_new_signal("56", "字符域", "测试字符域", "testChar", 0)
            create_new_signal("58", "Unicode域", "测试Unicode域", "testUnicode", 0)
            create_new_signal("62", "占位域", "测试占位域", "testSpace", 0)
            create_new_signal("66", "套表域", "测试套表域", "testTB", 0)
        # 4字节对齐
        elif align_way == "4":
            create_new_signal("24", "整型域", "测试整型域", "testInt", 1)
            create_new_signal("28", "Bit域", "测试Bit域", "testBit", 0)
            create_new_signal("36", "组合域", "测试组合域", "testZH", 0)
            create_new_signal("40", "Float域", "测试Float域", "testFloat", 0)
            create_new_signal("44", "布尔域", "测试布尔域", "testBool", 0)
            create_new_signal("48", "枚举域", "测试枚举域", "testEnum", 0)
            create_new_signal("52", "标签域", "测试标签域", "testLable", 0)
            create_new_signal("56", "字节域", "测试字节域", "testByte", 0)
            create_new_signal("64", "Double域", "测试字Double域", "testDouble", 0)
            create_new_signal("72", "BCD域", "测试字BCD域", "testBCD", 0)
            create_new_signal("76", "ASCII域", "测试字ASCII域", "testASCII", 0)
            create_new_signal("80", "字符域", "测试字符域", "testChar", 0)
            create_new_signal("84", "Unicode域", "测试Unicode域", "testUnicode", 0)
            create_new_signal("88", "占位域", "测试占位域", "testSpace", 0)
            create_new_signal("96", "套表域", "测试套表域", "testTB", 0)

    elif icd_type == "1394CCDL":
        # 1字节对齐
        if align_way == "1":
            create_new_signal("0", "整型域", "测试整型域", "testInt", 0)
            create_new_signal("1", "Bit域", "测试Bit域", "testBit", 0)
            create_new_signal("2", "组合域", "测试组合域", "testZH", 0)
            create_new_signal("4", "Float域", "测试Float域", "testFloat", 0)
            create_new_signal("9", "布尔域", "测试布尔域", "testBool", 0)
            create_new_signal("10", "枚举域", "测试枚举域", "testEnum", 0)
            create_new_signal("11", "标签域", "测试标签域", "testLable", 0)
            create_new_signal("12", "字节域", "测试字节域", "testByte", 0)
            create_new_signal("13", "Double域", "测试字Double域", "testDouble", 0)
            create_new_signal("21", "BCD域", "测试字BCD域", "testBCD", 0)
            create_new_signal("22", "ASCII域", "测试字ASCII域", "testASCII", 0)
            create_new_signal("23", "字符域", "测试字符域", "testChar", 0)
            create_new_signal("24", "Unicode域", "测试Unicode域", "testUnicode", 0)
            create_new_signal("28", "占位域", "测试占位域", "testSpace", 0)
        # 2字节对齐
        elif align_way == "2":
            create_new_signal("0", "整型域", "测试整型域", "testInt", 0)
            create_new_signal("2", "Bit域", "测试Bit域", "testBit", 0)
            create_new_signal("4", "组合域", "测试组合域", "testZH", 0)
            create_new_signal("6", "Float域", "测试Float域", "testFloat", 0)
            create_new_signal("12", "布尔域", "测试布尔域", "testBool", 0)
            create_new_signal("14", "枚举域", "测试枚举域", "testEnum", 0)
            create_new_signal("18", "标签域", "测试标签域", "testLable", 0)
            create_new_signal("20", "字节域", "测试字节域", "testByte", 0)
            create_new_signal("24", "Double域", "测试字Double域", "testDouble", 0)
            create_new_signal("34", "BCD域", "测试字BCD域", "testBCD", 0)
            create_new_signal("36", "ASCII域", "测试字ASCII域", "testASCII", 0)
            create_new_signal("38", "字符域", "测试字符域", "testChar", 0)
            create_new_signal("40", "Unicode域", "测试Unicode域", "testUnicode", 0)
            create_new_signal("44", "占位域", "测试占位域", "testSpace", 0)
        # 4字节对齐
        elif align_way == "4":
            create_new_signal("0", "整型域", "测试整型域", "testInt", 0)
            create_new_signal("4", "Bit域", "测试Bit域", "testBit", 0)
            create_new_signal("8", "组合域", "测试组合域", "testZH", 1)
            create_new_signal("12", "Float域", "测试Float域", "testFloat", 0)
            create_new_signal("16", "布尔域", "测试布尔域", "testBool", 2)
            create_new_signal("20", "枚举域", "测试枚举域", "testEnum", 0)
            create_new_signal("24", "标签域", "测试标签域", "testLable", 0)
            create_new_signal("28", "字节域", "测试字节域", "testByte", 0)
            create_new_signal("36", "Double域", "测试字Double域", "testDouble", 0)
            create_new_signal("44", "BCD域", "测试字BCD域", "testBCD", 0)
            create_new_signal("48", "ASCII域", "测试字ASCII域", "testASCII", 0)
            create_new_signal("52", "字符域", "测试字符域", "testChar", 0)
            create_new_signal("56", "Unicode域", "测试Unicode域", "testUnicode", 0)
            create_new_signal("60", "占位域", "测试占位域", "testSpace", 0)

    # 1553 只能2字节对齐
    elif icd_type == "1553":
        # 暂时未加套表
        create_new_signal("0", "整型域", "测试整型域", "testInt", 0)
        create_new_signal("0", "Bit域", "测试Bit域", "testBit", 1)
        create_new_signal("2", "组合域", "测试组合域", "testZH", 0)
        create_new_signal("4", "Float域", "测试Float域", "testFloat", 0)
        create_new_signal("8", "布尔域", "测试布尔域", "testBool", 2)
        create_new_signal("8", "枚举域", "测试枚举域", "testEnum", 3)
        create_new_signal("8", "标签域", "测试标签域", "testLable", 4)
        create_new_signal("8", "字节域", "测试字节域", "testByte", 5)
        create_new_signal("10", "Double域", "测试字Double域", "testDouble", 0)
        create_new_signal("18", "BCD域", "测试字BCD域", "testBCD", 0)
        create_new_signal("18", "ASCII域", "测试字ASCII域", "testASCII", 1)
        create_new_signal("20", "字符域", "测试字符域", "testChar", 0)
        create_new_signal("22", "字符串域", "测试字符串域", "testSring", 0)
        create_new_signal("26", "Unicode域", "测试Unicode域", "testUnicode", 0)
        create_new_signal("30", "占位域", "测试占位域", "testSpace", 0)

    elif icd_type == "422":
        # 1字节对齐
        if align_way == "1":
            create_new_signal("4", "整型域", "测试整型域", "testInt", 0)
            create_new_signal("5", "Bit域", "测试Bit域", "testBit", 0)
            create_new_signal("6", "组合域", "测试组合域", "testZH", 0)
            create_new_signal("8", "Float域", "测试Float域", "testFloat", 6)
            create_new_signal("12", "布尔域", "测试布尔域", "testBool", 0)
            create_new_signal("12", "枚举域", "测试枚举域", "testEnum", 1)
            create_new_signal("12", "标签域", "测试标签域", "testLable", 2)
            create_new_signal("13", "字节域", "测试字节域", "testByte", 0)
            # create_new_signal("14", "Double域", "测试字Double域", "testDouble", 0) # 1字节位置不够不能建
            create_new_signal("14", "BCD域", "测试字BCD域", "testBCD", 0)
            create_new_signal("15", "ASCII域", "测试字ASCII域", "testASCII", 0)
            create_new_signal("16", "字符域", "测试字符域", "testChar", 1)
            create_new_signal("17", "占位域", "测试占位域", "testSpace", 0)
            # 位置不够以下信号不能建，包括套表
            # create_new_signal("17", "字符串域", "测试字符串域", "testSring", 0)
            # create_new_signal("17", "Unicode域", "测试Unicode域", "testUnicode", 0)

        # 2字节对齐
        elif align_way == "2":
            create_new_signal("4", "整型域", "测试整型域", "testInt", 0)
            create_new_signal("4", "Bit域", "测试Bit域", "testBit", 1)
            create_new_signal("6", "组合域", "测试组合域", "testZH", 0)
            create_new_signal("8", "Float域", "测试Float域", "testFloat", 6)
            create_new_signal("12", "布尔域", "测试布尔域", "testBool", 0)
            create_new_signal("12", "枚举域", "测试枚举域", "testEnum", 1)
            create_new_signal("12", "标签域", "测试标签域", "testLable", 2)
            create_new_signal("12", "字节域", "测试字节域", "testByte", 3)
            create_new_signal("14", "Double域", "测试字Double域", "testDouble", 0)
            create_new_signal("14", "BCD域", "测试字BCD域", "testBCD", 0)
            create_new_signal("14", "ASCII域", "测试字ASCII域", "testASCII", 0)
            create_new_signal("14", "字符域", "测试字符域", "testChar", 1)
            create_new_signal("16", "占位域", "测试占位域", "testSpace", 1)
            # 位置不够以下信号不能建，包括套表
            # create_new_signal("16", "字符串域", "测试字符串域", "testSring", 0)
            # create_new_signal("16", "Unicode域", "测试Unicode域", "testUnicode", 1)
        # 4字节对齐
        elif align_way == "4":
            create_new_signal("4", "整型域", "测试整型域", "testInt", 0)
            create_new_signal("4", "Bit域", "测试Bit域", "testBit", 1)
            create_new_signal("4", "组合域", "测试组合域", "testZH", 2)
            create_new_signal("8", "Float域", "测试Float域", "testFloat", 6)
            create_new_signal("12", "布尔域", "测试布尔域", "testBool", 0)
            create_new_signal("12", "枚举域", "测试枚举域", "testEnum", 1)
            create_new_signal("12", "标签域", "测试标签域", "testLable", 2)
            create_new_signal("12", "字节域", "测试字节域", "testByte", 3)
            create_new_signal("12", "BCD域", "测试字BCD域", "testBCD", 4)
            create_new_signal("12", "ASCII域", "测试字ASCII域", "testASCII", 5)
            # 以下长度不够不能建，包括套表
            # create_new_signal("14", "Double域", "测试字Double域", "testDouble", 0)
            # create_new_signal("12", "字符域", "测试字符域", "testChar", 6)
            # create_new_signal("16", "字符串域", "测试字符串域", "testSring", 0) # 1字节位置不够不能建字符串域
            # create_new_signal("16", "Unicode域", "测试Unicode域", "testUnicode", 1) # 1字节位置不够不能建Unicode域
            # create_new_signal("12", "占位域", "测试占位域", "testSpace", 6)

    if icd_type == "TTE":
        # 1字节对齐
        if align_way == "1":
            pass
        # 2字节对齐
        elif align_way == "2":
            pass
        # 4字节对齐
        elif align_way == "4":
            pass
        pass


if __name__ == '__main__':
    main_win.child_window(title="域信息", control_type="TabItem").click_input()
    main_win.child_window(title="显示空白", control_type="Button").click_input()
    # create_new_signal("24", "整型域", "测试整型域", "testInt", 1)
    create_new_signal("52", "套表域", "测试套表域", "testTB", 0)

